<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Statistics;
use think\facade\View;

class Desktop extends Base
{
    public function statistics(): string
    {
        $list = Statistics::select()->toArray();
        $income = Statistics::where('key', 'income')->find()->toArray();
        $expend = Statistics::where('key', 'expend')->find()->toArray();
        $ratio_key = ['today', 'month', 'year', 'sum', 'yesterday', 'last_month'];
        $ratio['title'] = '拨比率';
        foreach ($ratio_key as $v) {
            if ($income[$v] > 0 && $expend[$v] > 0) {
                $ratio[$v] = sprintf('%.2f', ($expend[$v] / $income[$v] * 100)) . '%';
            } else {
                $ratio[$v] = '0.00%';
            }
        }
        $list[] = $ratio;
        $follow = \app\common\model\Product::where('is_del',0)->count();
        View::assign('follow', $follow);
        $follow2 = \app\common\model\Product::where('is_del',1)->count();
        View::assign('follow2', $follow2);
        View::assign('list', $list);
        return View::fetch();
    }

}
